package servlet;

import com.alibaba.fastjson.JSON;
import entity.Cart;
import entity.Msg;
import entity.Product;
import service.ICartService;
import service.IProductService;
import service.imp.CartService;
import service.imp.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author Jiuyuesheng
 * @date 2019/12/29  15:48
 */

public class CartServlet extends HttpServlet {
    private ICartService cartService = new CartService();
    private IProductService productService = new ProductService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        if ("add".equals(method)) {
            doAddCart(request, response);
        } else if ("del".equals(method)) {
            doDelCart(request, response);
        } else if ("update".equals(method)) {
            doUpdate(request, response);
        } else if ("query".equals(method)) {
            doQuery(request, response);
        }else if("queryName".equals(method)){
            doQueryName(request,response);
        }
    }

    private void doQueryName(HttpServletRequest request, HttpServletResponse response) {
        int pid = Integer.parseInt(request.getParameter("pid"));

        Product product = productService.queryProductByPid(pid);
        String pname = product.getPname();
        Msg msg = new Msg();
        msg.setStatus(200);
        msg.setData(pname);

        try {
            response.getWriter().print(JSON.toJSONString(msg));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int getUid(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        int uid = -1;
        //查询当前用户的uid
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("uid".equals(cookie.getName())) {
                    uid = Integer.valueOf(cookie.getValue());
                    System.out.println(cookie.getPath());
                    System.out.println("cookie:"+cookie.getValue());
                }
            }
        }
        return uid;
    }

    private void doAddCart(HttpServletRequest request, HttpServletResponse response) {
        String msg = null;
        //1.先查找当前用户的购物车的信息
        int uid = getUid(request);
        List<Cart> carts = cartService.queryAllCart(uid);

        //判断商品是否已经存在购物车中
        int pid = Integer.valueOf(request.getParameter("pid"));
        //判断商品数量
        Product product = productService.queryProductByPid(pid);
        int count = product.getCount();
        if (count < 1) {
            Msg msg1 = new Msg();
            msg1.setStatus(400);
            msg1.setData("库存为0,添加失败");
            msg = JSON.toJSONString(msg1);
            try {
                response.getWriter().print(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }
        if (carts != null) {
            boolean flag = true;
            for (Cart cart : carts) {
                if (pid == cart.getPid()) {
                    cart.setCount(cart.getCount() + 1);
                    msg = cartService.updateCart(cart);
                    flag = false;
                }
            }
            if(flag == true){
                Cart cart1 = new Cart();
                cart1.setPid(pid);
                cart1.setUid(uid);
                cart1.setCount(1);
                cart1.setCreatetime(new Date());
                msg = cartService.addCart(cart1);
            }
        } else {
            Cart cart1 = new Cart();
            cart1.setPid(pid);
            cart1.setUid(uid);
            cart1.setCount(1);
            cart1.setCreatetime(new Date());
            msg = cartService.addCart(cart1);
        }
        try {
            response.getWriter().print(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void doDelCart(HttpServletRequest request, HttpServletResponse response) {
        int cid = Integer.parseInt(request.getParameter("cid"));
        String msg = cartService.deleteCart(cid);
        try {
            response.getWriter().print(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void doUpdate(HttpServletRequest request, HttpServletResponse response) {

    }

    private void doQuery(HttpServletRequest request, HttpServletResponse response) {
        int uid = getUid(request);
        System.out.println("uid:"+uid);
        List<Cart> carts = cartService.queryAllCart(uid);
        Msg msg = new Msg();
        msg.setStatus(200);
        msg.setData(carts);
        try {
            response.getWriter().print(JSON.toJSONString(msg));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
